﻿using System;
using System.Linq;
using System.Web.Mvc;

using Guoc.BigMall.Infrastructure.Extension;
using Guoc.BigMall.Infrastructure.File;
using Guoc.BigMall.Admin.Services;
using Guoc.BigMall.Application;
using Guoc.BigMall.Application.DTO;
using Guoc.BigMall.Application.Facade;
using Guoc.BigMall.Application.Search;
using Guoc.BigMall.Application.ViewObject;
using Guoc.BigMall.Domain.ValueObject;
using Dapper.DBContext;
using Newtonsoft.Json;
using Guoc.BigMall.Domain.Entity;

namespace Guoc.BigMall.Admin.Controllers
{
    [Permission]
    public class SaleOrderController : Controller
    {
        IExcel _excelService;
        IDBContext _db;
        ISaleOrderFacade _saleOrderFacade;     
        IContextFacade _context;
        public SaleOrderController(IDBContext db, ISaleOrderFacade saleOrderFacade, IExcel iexcel, IContextFacade context)
        {
            this._excelService = iexcel;
            this._db = db;
            this._saleOrderFacade = saleOrderFacade;
            this._context = context;
        }
        #region 销售订单
        public ActionResult Index()
        {
           // ViewBag.ShowStatus = (int)SaleOrderStatus.Create;
          //  ViewBag.OrderType = (int)OrderType.Order;
            ViewBag.payStatusOptions = JsonConvert.SerializeObject(typeof(OrderPayStatus).GetValueToDescription().ToArray()); 
            return View();
        }

        public ActionResult GetSaleOrderList(Pager page, SearchSaleOrder condition)
        {
            if (condition.StoreId.IsNullOrEmpty())
                condition.StoreId = _context.CurrentAccount.CanViewStores;

            var rows = _saleOrderFacade.GetList(page, condition);

            if (page.toExcel)
            {
                var data = _excelService.WriteToExcelStream(rows.ToList(), ExcelVersion.Above2007, false, true).ToArray();
                var fileName = string.Format("销售订单_{0}.xlsx", DateTime.Now.ToString("yyyyMMdd"));
                return File(data, "application/ms-excel", fileName);
            }

            return Json(new { success = true, data = rows, total = page.Total });
        }

        public ActionResult CreateSo()
        {
          //  ViewBag.StatusDesc = SaleOrderStatus.Create.Description();
            ViewBag.CreatedBy = _context.CurrentAccount.AccountId;
            ViewBag.CreatedByName = _context.CurrentAccount.NickName;
            ViewBag.DefaultStoreId = _context.CurrentAccount.StoreArray.FirstOrDefault();
            return View();
        }


        [HttpPost]
        public JsonResult CreateSo(SaleOrderModel model)
        {
            model.UpdatedBy = _context.CurrentAccount.AccountId;
            model.UpdatedByName = _context.CurrentAccount.NickName;
            model.CreatedBy = _context.CurrentAccount.AccountId;

          //  _saleOrderFacade.CreateSaleOrder(model);

            return Json(new { success = true });
        }

        public ActionResult AuditIndex()
        {
            ViewBag.OrderType = (int)OrderType.Order;
            ViewBag.ShowStatus = (int)SaleOrderStatus.WaitToPay;
            return View();
        }

        [HttpPost]
        public ActionResult Audit(SaleOrderAuditedModel model)
        {
            if (model.OrderCode.IsEmpty())
                return Json(new { success = false, error = "订单编号不能为空" });

          //  _saleOrderFacade.AuditedSaleOrder(model);

            return Json(new { success = true });
        }

        [HttpPost]
        public ActionResult Reject(SaleOrderRejectModel model)
        {
            if (model.OrderCode.IsEmpty())
                return Json(new { success = false, error = "订单编号不能为空" });

         //   _saleOrderFacade.RejectSaleOrder(model);

            return Json(new { success = true });
        }

        public ActionResult ShowDetail(int id)
        {
            var order = _saleOrderFacade.GetOrderDetail(id);
            ViewBag.SaleOrder = JsonConvert.SerializeObject(order);

            return View();
        }

        public ActionResult Edit(string code)
        {
            var condition = new SearchSaleOrder() { OrderCode = code };
           // var order = _saleOrderFacade.GetSaleOrderModel(condition);
           // ViewBag.SaleOrder = JsonConvert.SerializeObject(order);
            return View();
        }

        [HttpPost]
        public JsonResult Edit(SaleOrderModel saleOrderModel)
        {
          //  _saleOrderFacade.UpdateSaleOrder(saleOrderModel);
            return Json(new { success = true });
        }
        //public ActionResult Print(int id)
        //{
        //    var condition = new SearchSaleOrder() { SaleOrderId = id };
        //    var order = _saleOrderFacade.GetSaleOrderModel(condition);


        //    return PartialView("SaleOrderTemplate", order);
        //}
        #endregion
      
    }
}